﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web.UI;
using RCSP.Biz.Models;
using RCSP.Biz.Presenters;
using RCSP.Biz.Views;
using RCSP.Common;
using System.Web.UI.WebControls;
using System.IO;

namespace RCSP.FrontEnd.Pages
{
    public partial class MemberRegister : Page, IMemberView, IPresetView
    {
        private MemberPresenter memPre;

        public MemberPresenter MemberPre
        {
            get
            {
                if (memPre == null)
                {
                    memPre = new MemberPresenter(this);
                }
                return memPre;
            }
            set { memPre = value; }
        }

        private PresetPresenter presetPre;
        public PresetPresenter PresetPre
        {
            get
            {
                if (presetPre == null)
                {
                    presetPre = new PresetPresenter(this);
                }
                return presetPre;
            }
            set { presetPre = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            txtZipCode.Attributes.Add("onkeypress", "EnterEvent(" + btnZipCode.ClientID + ",event)");
            txtAreaCode.Attributes.Add("onkeypress", "EnterEvent(" + btnArea.ClientID + ",event)");
            txtBankNo.Attributes.Add("onkeypress", "EnterEvent(" + btnBank.ClientID + ",event)");
            txtBranchNo.Attributes.Add("onkeypress", "EnterEvent(" + btnBranch.ClientID + ",event)");

            if (!IsPostBack)
            {
                LoadDataToDropDownList();
                BindDataToGridView();
            }

        }

        private void GetData()
        {
            Mode = ControlMode.View;
            MemberPre.DoAction();
        }

        #region IView Members

        public ControlMode Mode { get; set; }

        #endregion

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            GetData();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {

            Mode = ControlMode.Create;
            MemberPre.DoAction();
        }

        #region IMemberView Members

        public string AccountNumber
        {
            get
            {
                return txtAccountNumber.Text;
            }
            set
            {
                if (value != null)
                {
                    txtAccountNumber.Text = value;
                }
            }
        }

        public string KanaName
        {
            get
            {
                return txtKanaName.Text;
            }
            set
            {
                if (value != null)
                {
                    txtKanaName.Text = value;
                }
            }
        }

        public string Name
        {
            get
            {
                return txtName.Text;
            }
            set
            {
                if (value != null)
                {
                    txtName.Text = value;
                }
            }
        }

        public string GenderClassification
        {
            get
            {
                return rbtnGender.SelectedValue;
            }
            set
            {

                if (!string.IsNullOrEmpty(value))
                {
                    try
                    {
                        rbtnGender.Items.FindByValue(value).Selected = true;
                    }
                    catch { }
                }
            }
        }

        public DateTime? DateOfBirth
        {
            get { return Helper.ToNullableDateTime(txtBirthDay.Text); }
            set
            {
                if (value != null)
                {
                    txtBirthDay.Text = value.Value.ToString("yyyy/MM/dd");
                }
            }
        }
        public int? DateOfBirthYear
        {
            get
            {
                return Helper.ToNullableInt32(txtAge.Text);
            }
            set
            {
                if (value != null)
                {
                    txtAge.Text = value.Value.ToString();
                }
            }
        }

        public int? DateOfBirthMonth
        {
            get
            {
                return Helper.ToNullableInt32(txtMonth.Text);
            }
            set
            {
                if (value != null)
                {
                    txtMonth.Text = value.ToString();
                }
            }
        }

        public int? DateOfBirthDay
        {
            get
            {
                return Helper.ToNullableInt32(txtDate.Text);
            }
            set
            {
                if (value != null)
                {
                    txtDate.Text = value.ToString();
                }
            }
        }
        public string Zip
        {
            get
            {
                return txtZipCode.Text;
            }
            set
            {
                if (value != null)
                {
                    txtZipCode.Text = value;
                }
            }
        }

        public string Address1
        {
            get
            {
                return txtLivingPlant.Text;
            }
            set
            {
                if (value != null)
                {
                    txtLivingPlant.Text = value;
                }
            }
        }

        public string PhoneNumber
        {
            get
            {
                return txtPhoneNumber.Text;
            }
            set
            {
                if (value != null)
                {
                    txtPhoneNumber.Text = value;
                }
            }
        }

        public string Address2
        {
            get
            {
                return rbtnAdressClassification.SelectedValue;
            }
            set
            {

                if (!string.IsNullOrEmpty(value))
                {
                    try
                    {
                        rbtnAdressClassification.Items.FindByValue(value.ToString()).Selected = true;
                    }
                    catch
                    { }
                }

            }
        }

        public string BillingDepartmentJobTitle
        {
            get
            {
                return txtDepartmentDevision.Text;
            }
            set
            {
                if (value != null)
                {
                    txtDepartmentDevision.Text = value;
                }
            }
        }

        public string EmployerResponsibleName
        {
            get
            {
                return txtChiefName.Text;
            }
            set
            {
                if (value != null)
                {
                    txtChiefName.Text = value;
                }
            }
        }


        public DateTime? WithdrawalDate
        {
            get
            {
                return Helper.ToNullableDateTime(txtWithDrawDate.Text);
            }
            set
            {
                if (value != null)
                {
                    txtWithDrawDate.Text = value.Value.ToString("yyyy/MM/dd");
                }
            }
        }

        public DateTime? ChangeProcessDate
        {
            get
            {
                return Helper.ToNullableDateTime(txtChangeProcessDate.Text);
            }
            set
            {
                if (value != null)
                {
                    txtChangeProcessDate.Text = value.Value.ToString("yyyy/MM/dd");
                }
            }
        }

        public string ReasonForStoppingUse
        {
            get
            {
                return txtReasonStop.Text;
            }
            set
            {
                if (value != null)
                {
                    txtReasonStop.Text = value;
                }
            }
        }
        public string AreaCode
        {
            get
            {
                return txtAreaCode.Text;
            }
            set
            {
                if (!string.IsNullOrEmpty(value))
                {
                    txtAreaCode.Text = value;
                }
            }
        }

        public string SouvenirPass
        {
            get
            {
                return ddlSouvenirPass.SelectedValue;
            }
            set
            {

                if (!string.IsNullOrEmpty(value))
                {
                    ddlSouvenirPass.Items.FindByValue(value).Selected = true;
                }
            }
        }
        public string UG
        {
            get
            {
                return ddlUG.SelectedValue;
            }
            set
            {

                if (!string.IsNullOrEmpty(value))
                {
                    ddlUG.Items.FindByValue(value).Selected = true;
                }
            }
        }

        public string RemarkCate4
        {
            get
            {
                return ddlRemarksCate4.SelectedValue;
            }
            set
            {
                List<Preset> presets = PresetPre.GetPresetByType(3804);
                if (presets != null && presets.Count > 0)
                {
                    ddlRemarksCate4.DataSource = presets;
                    ddlRemarksCate4.DataValueField = "Code";
                    ddlRemarksCate4.DataTextField = "NameX";
                    ddlRemarksCate4.DataBind();
                }
                if (!string.IsNullOrEmpty(value))
                {
                    ddlRemarksCate4.Items.FindByValue(value).Selected = true;
                }
            }
        }

        public string RemarkCate5
        {
            get
            {
                return ddlRemarksCate5.SelectedValue;
            }
            set
            {

                if (!string.IsNullOrEmpty(value))
                {
                    ddlRemarksCate5.Items.FindByValue(value).Selected = true;
                }
            }
        }

        public string BillingCode
        {
            get
            {
                return rbtnBillingType.SelectedValue;
            }
            set
            {
                if (!string.IsNullOrEmpty(value))
                {
                    rbtnBillingType.Items.FindByValue(value.Trim()).Selected = true;
                }
            }
        }

        public string BillingClassification
        {
            get
            {
                return rbtnClaismDivision.SelectedValue;
            }
            set
            {
                if (!string.IsNullOrEmpty(value))
                {
                    rbtnClaismDivision.Items.FindByValue(value.Trim()).Selected = true;
                }
            }
        }

        public string PaymentMethod
        {
            get
            {
                return rbtnPaymentMethod.SelectedValue;
            }
            set
            {
                if (!string.IsNullOrEmpty(value))
                {
                    rbtnPaymentMethod.Items.FindByValue(value).Selected = true;
                }
            }
        }

        public string PaymentDivision
        {
            get
            {
                return rbtnClaimType.SelectedValue;
            }
            set
            {
                if (!string.IsNullOrEmpty(value))
                {
                    rbtnClaimType.Items.FindByValue(value.Trim()).Selected = true;
                }
            }
        }

        public int? YearBillingMonth
        {
            get
            {
                return Helper.ToNullableInt32(txtYearMonthPayment.Text);
            }
            set
            {
                if (value != null)
                {
                    txtYearMonthPayment.Text = value.ToString();
                }
            }
        }

        public int? NumberOfUnits
        {
            get;

            set;

        }

        public int? BankCode
        {
            get
            {
                return Helper.ToNullableInt32(txtBankNo.Text);
            }
            set
            {
                if (value != null)
                {
                    txtBankNo.Text = value.ToString();
                }
            }
        }
        public string BankName
        {

            get { return txtBank.Text; }
            set
            {
                if (!string.IsNullOrEmpty(value))
                {

                    txtBank.Text = value;
                }
            }
        }

        public int? BranchCode
        {
            get
            {
                return Helper.ToNullableInt32(txtBranchNo.Text);
            }
            set
            {
                if (value != null)
                {
                    txtBranchNo.Text = value.ToString();
                }
            }
        }



        public string DepositEvent
        {
            get
            {
                return rbtnDeposit.SelectedValue;
            }
            set
            {
                if (!string.IsNullOrEmpty(value))
                {
                    rbtnDeposit.Items.FindByValue(value.Trim()).Selected = true;
                }
            }
        }

        public string Account
        {
            get
            {
                return txtAccount.Text;
            }
            set
            {
                if (value != null)
                {
                    txtAccount.Text = value;
                }
            }
        }

        public string ParentMemberShipNumber
        {
            get
            {
                return txtPrimaryMember.Text;
            }
            set
            {
                if (value != null)
                {
                    txtPrimaryMember.Text = value;
                }
            }
        }

        public string WorkPostalCode
        {
            get
            {
                return txtZipCodeDisplay.Text;
            }
            set
            {
                if (value != null)
                {
                    txtZipCodeDisplay.Text = value;
                }
            }
        }


        public string WorkAddress2
        {
            get
            {
                return txtLivingPlantShow.Text;
            }
            set
            {
                if (value != null)
                {
                    txtLivingPlantShow.Text = value;
                }
            }
        }

        public string WorkPhoneNumber
        {
            get
            {
                return txtPhoneNumberShow.Text;
            }
            set
            {
                if (value != null)
                {
                    txtPhoneNumberShow.Text = value;
                }
            }
        }

        public string MemberClassification
        {
            get
            {
                return ddlMembershipClassification.SelectedValue;
            }
            set
            {

                if (!string.IsNullOrEmpty(value))
                {
                    try
                    {
                        ddlMembershipClassification.Items.FindByValue(value).Selected = true;
                    }
                    catch { }
                }
            }
        }

        public string MemberType
        {
            get
            {
                return ddlMemberRank.SelectedValue;
            }
            set
            {

                if (!string.IsNullOrEmpty(value))
                {
                    ddlMemberRank.Items.FindByValue(value).Selected = true;
                }
            }

        }

        public int? DMDestination
        {
            get
            {
                return Helper.ToNullableInt32(rbtnDM.SelectedValue);
            }
            set
            {
                if (value != null)
                {
                    rbtnDM.Items.FindByValue(value.ToString()).Selected = true;
                }
            }
        }

        public DateTime? JoinDate
        {
            get;
            set;
        }

        public string Hospital
        {
            get
            {
                return txtPrimaryCareDoctor.Text;
            }
            set
            {
                if (value != null)
                {
                    txtPrimaryCareDoctor.Text = value;
                }
            }
        }

        public string Contact
        {
            get
            {
                return txtContactInfo.Text;
            }
            set
            {
                if (value != null)
                {
                    txtContactInfo.Text = value;
                }
            }
        }

        public string Note1
        {
            get;
            set;
        }

        public string Note2
        {
            get;
            set;
        }

        public string Note3
        {
            get;
            set;
        }

        public string Note4
        {
            get;
            set;
        }

        public string PhotoFileName
        {
            get
            {
                return lblFileName.Text;
            }
            set
            {
                if (value != null)
                {
                    lblFileName.Text = value;
                }
            }
        }

        public DateTime? PhotoDate
        {
            get
            {
                return Helper.ToNullableDateTime(txtUpdatedDate.Text);
            }
            set
            {
                if (value != null)
                {
                    txtUpdatedDate.Text = value.Value.ToString("yyyy/MM/dd");
                }
            }
        }

        public string AccountName
        {
            get
            {
                return txtAccountHolder.Text;
            }
            set
            {
                if (value != null)
                {
                    txtAccountHolder.Text = value;
                }
            }
        }

        public string DateOfBirthEraClassification
        {
            get
            {
                return ddlPeriod.SelectedValue;
            }
            set
            {

                if (!string.IsNullOrEmpty(value))
                {
                    ddlPeriod.Items.FindByValue(value).Selected = true;
                }
            }
        }

        public string UseCourse
        {
            get { return txtUse.Text; }
            set
            {
                if (!string.IsNullOrEmpty(value))
                {
                    txtUse.Text = value;
                }
            }
        }

        public string District
        {
            get { return txtDistrict.Text; }
            set
            {
                if (!string.IsNullOrEmpty(value))
                {
                    txtDistrict.Text = value;
                }
            }
        }

        #endregion

        #region IPresetView Members

        public int PresetType
        {
            get;
            set;
        }

        public string NameX
        {
            get;
            set;
        }

        public int Code
        {
            get;
            set;
        }


        public string BranchName
        {
            get { return txtBranch.Text; }

            set
            {
                if (!string.IsNullOrEmpty(value))
                {
                    txtBranch.Text = value;
                }
            }
        }
        public string TownCollege
        {
            get { return txtTownAndCollege.Text; }
            set
            {
                if (value != null)
                {
                    txtTownAndCollege.Text = value;
                }
            }
        }
        private void LoadDataToDropDownList()
        {

            List<Preset> presets = PresetPre.GetPresetByType(2200);
            if (presets != null && presets.Count > 0)
            {
                ddlPeriod.DataSource = presets;
                ddlPeriod.DataTextField = "NameX";
                ddlPeriod.DataValueField = "Code";
                ddlPeriod.DataBind();
            }

            List<Preset> memberRanks = PresetPre.GetPresetByType(3801);
            if (memberRanks != null && memberRanks.Count > 0)
            {
                ddlMemberRank.DataSource = memberRanks;
                ddlMemberRank.DataValueField = "Code";
                ddlMemberRank.DataTextField = "NameX";
                ddlMemberRank.DataBind();
            }

            List<Preset> memberShips = PresetPre.GetPresetByType(2500);
            if (memberShips != null && memberShips.Count > 0)
            {
                ddlMembershipClassification.DataSource = memberShips;
                ddlMembershipClassification.DataValueField = "Code";
                ddlMembershipClassification.DataTextField = "NameX";
                ddlMembershipClassification.DataBind();
            }

            List<Preset> genders = PresetPre.GetPresetByType(3100);
            if (genders != null && genders.Count > 0)
            {
                var listGender = new List<Preset>();
                foreach (var gen in genders)
                {
                    if (!string.IsNullOrEmpty(gen.NameX))
                    {
                        listGender.Add(gen);
                    }
                }
                rbtnGender.DataSource = listGender;
                rbtnGender.DataValueField = "Code";
                rbtnGender.DataTextField = "NameX";
                rbtnGender.DataBind();
                rbtnGender.Items[0].Selected = true;
            }

            List<Preset> remark5s = PresetPre.GetPresetByType(3805);
            if (remark5s != null && remark5s.Count > 0)
            {
                ddlRemarksCate5.DataSource = remark5s;
                ddlRemarksCate5.DataValueField = "Code";
                ddlRemarksCate5.DataTextField = "NameX";
                ddlRemarksCate5.DataBind();
            }

            List<Preset> uGs = PresetPre.GetPresetByType(3803);
            if (uGs != null && uGs.Count > 0)
            {
                ddlUG.DataSource = uGs;
                ddlUG.DataValueField = "Code";
                ddlUG.DataTextField = "NameX";
                ddlUG.DataBind();
            }
            List<Preset> souvernirs = PresetPre.GetPresetByType(3802);
            if (souvernirs != null && souvernirs.Count > 0)
            {
                ddlSouvenirPass.DataSource = souvernirs;
                ddlSouvenirPass.DataValueField = "Code";
                ddlSouvenirPass.DataTextField = "NameX";
                ddlSouvenirPass.DataBind();
            }

            List<Preset> address2 = PresetPre.GetPresetByType(3101);
            if (address2 != null && address2.Count > 0)
            {
                var listAddress = new List<Preset>();
                foreach (var gen in address2)
                {
                    if (!string.IsNullOrEmpty(gen.NameX))
                    {
                        listAddress.Add(gen);
                    }
                }
                rbtnAdressClassification.DataSource = listAddress;
                rbtnAdressClassification.DataValueField = "Code";
                rbtnAdressClassification.DataTextField = "NameX";
                rbtnAdressClassification.DataBind();
                rbtnAdressClassification.Items[0].Selected = true;

            }

        }

        #endregion

        public void BindDataToGridView()
        {
            DataTable table = new DataTable();
            table.Columns.Add("Number", typeof(int));
            table.Columns.Add("AvailableStartDate", typeof(DateTime));
            table.Columns.Add("PaymentDivision", typeof(string));
            table.Columns.Add("BillingMonth", typeof(int));
            table.Rows.Add(182, DateTime.Now, "年払", 1);
            table.Rows.Add(183, DateTime.Now, "年払", 2);
            gvContractLocker.DataSource = table;
            gvContractLocker.DataBind();
        }

        protected void btnZipCode_Click(object sender, EventArgs e)
        {
            bool isExist = MemberPre.BindAdressToTextBox();
            if (isExist)
            {
                txtLivingPlant.Focus();
                txtZipCode.BackColor = System.Drawing.ColorTranslator.FromHtml("white");
                txtZipCode.Attributes.Remove("Title");
            }
            else
            {
                txtZipCode.BackColor = System.Drawing.ColorTranslator.FromHtml("#ff69b4");
                //txtZipCode.Attributes.Add("Title", "該当デーがが存在していません。");
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Msg", "alert('該当データが存在していません。')", true);
                txtZipCode.Focus();
            }

        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {
            if (imgUpload.HasFile) 
            {
            
            }
        }

        protected void btnArea_Click(object sender, EventArgs e)
        {
            bool isExist = MemberPre.BindDistrictName();
            if (!isExist)
            {
                txtAreaCode.BackColor = System.Drawing.ColorTranslator.FromHtml("#ff69b4");
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Msg", "alert('該当データが存在していません。')", true);
                txtAreaCode.Focus();
            }
            else
            {
                txtAreaCode.BackColor = System.Drawing.ColorTranslator.FromHtml("white");
                txtAreaCode.Attributes.Remove("Title");
            }
        }

        protected void btnBank_Click(object sender, EventArgs e)
        {
            bool isExist = MemberPre.BindBankName();
            if (!isExist)
            {
                txtBankNo.BackColor = System.Drawing.ColorTranslator.FromHtml("#ff69b4");
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Msg", "alert('該当データが存在していません。')", true);
                txtBankNo.Focus();
            }
            else
            {
                txtBankNo.BackColor = System.Drawing.ColorTranslator.FromHtml("white");
                txtBankNo.Attributes.Remove("Title");
            }
        }

        protected void btnBranch_Click(object sender, EventArgs e)
        {
            bool isExist = MemberPre.BindBranchName();
            if (!isExist)
            {
                txtBranchNo.BackColor = System.Drawing.ColorTranslator.FromHtml("#ff69b4");
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Msg", "alert('該当データが存在していません。')", true);
                txtBranchNo.Focus();
            }
            else
            {
                txtBranchNo.BackColor = System.Drawing.ColorTranslator.FromHtml("white");
                txtBranchNo.Attributes.Remove("Title");
            }
        }

        //protected void btnPicture_Click(object sender, ImageClickEventArgs e)
        //{
        //    if (imgUpload.HasFile) 
        //    {
        //        try
        //        {
        //            string filename = Path.GetFileName(imgUpload.FileName);
        //            imgUpload.SaveAs(Server.MapPath("~/Images/") + filename);
        //            lblFileName.Text = filename;
        //            btnPicture.ImageUrl = Server.MapPath("~/Images/") + filename;
        //            //StatusLabel.Text = "Upload status: File uploaded!";
        //        }
        //        catch (Exception ex)
        //        {
        //           //StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
        //        }
        //    }
        //}
    }
}